#include <stdlib.h>
#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_mixer.h"
#include "SDL_image.h"

#include "torainguru.h"
#include "input.h"
#include "draw.h"

#ifdef PSP
	#include <pspkernel.h>
	#include <pspdebug.h>
	#include <callbacks.h>
	#include "pspconnect.h"
	#include "game.h"
	PSP_MODULE_INFO("TORAINGURU", 0, 1, 0);
	//PSP_HEAP_SIZE_KB(20480);
	//PSP_HEAP_SIZE_MAX();

	#define printf pspDebugScreenPrintf
#endif



#ifdef PSP
int  main()
#else
int main(int argc, char* argv[])
#endif
{
	#ifdef PSP
		//SceUID thid;
		//SceUID thid2;
		pspDebugScreenInit();
		SetupCallbacks();
		sceUtilityLoadNetModule(1);
	    sceUtilityLoadNetModule(3);
		thid = sceKernelCreateThread("net_thread", net_thread, 0x11, 0x10000, PSP_THREAD_ATTR_USER, NULL);
		if(thid < 0)
		{
			printf("Error, could not create thread\n");
			sceKernelSleepThread();
		}
		thid2 = sceKernelCreateThread("game", play, 0x11, 256*1024, PSP_THREAD_ATTR_USER, NULL);
		if(thid2 < 0)
		{
			printf("Error, could not create thread\n");
			sceKernelSleepThread();
		}
	#endif
	
	i = SDL_Init(SDL_INIT_VIDEO | 
		SDL_INIT_AUDIO | 
	    SDL_INIT_TIMER |
	    SDL_INIT_JOYSTICK );
  	if (i != 0) 
  	{
		#ifdef DEBUG
    		printf("SDL initialize error.\n");
		#endif
    		exit(-1);
  	}
  	InputInit();
  	buff2_mut = SDL_CreateMutex();
  	screen = SDL_SetVideoMode(480, 272, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT);
  	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
  	loadResourses();
  	#ifdef PSP
		sceKernelStartThread(thid, 0, NULL);
		sceKernelStartThread(thid2, 0, NULL);
		sceKernelSleepThread();
		
	#else
  		while (TRUE && !AppExit)
    	{
   			InputPoll();
   		 	drawPlayer(player);
   		 	SDL_Flip(screen);
    }
    #endif
    SDL_FreeSurface(player->source);
    InputFree();
    SDL_Quit();
    #ifdef PSP
    sceKernelExitDeleteThread(0);
    #endif
 	return(0);
}
